/**
 *  Copyright (c) 2017-2022 Rushang Co.Ltd All Rights Reserved.
 */
package com.rshang.rsdp.router;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.UnauthorizedException;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.rshang.rsdp.annotation.Log;
import com.rshang.rsdp.entity.TbUser;
import com.rshang.rsdp.kit.StrKits;
import com.rshang.rsdp.service.ITbRsdlRyxxService;
import com.rshang.rsdp.service.ITbUserService;

/**
 * 功能简介：主路由控制器<br/>
 * @author zhangjun
 * @create 2017年8月10日下午9:58:28
 * 
 */
@Controller
public class IndexRouter extends BaseRouter{

	//private static final Logger logger = LoggerFactory.getLogger(IndexRouter.class);
	@Autowired
	ITbUserService userService;
	
	@Autowired
	ITbRsdlRyxxService ryxxService;
	/**
	 * 系统首页
	 * @throws JsonProcessingException 
	 * 
	 */
	@RequestMapping("/") 
	public ModelAndView index(HttpServletRequest request) throws JsonProcessingException{
		ModelAndView view = new ModelAndView("index");
		Subject currentUser = SecurityUtils.getSubject();
		if(currentUser.isAuthenticated())
		{
			view.setViewName("main");
			TbUser user = getSessionUser();
			view.addObject("menuTree",userService.getJsonMenuByUserId(user.getUserId()));//左侧菜单
			view.addObject("moduleNames",userService.getModulesByUserId(user.getUserId()));//模块显示
		}
	    return view;
	}
	
	/**
	 * 系统首页
	 * @throws JsonProcessingException 
	 */
	@RequestMapping("/main")
	public ModelAndView main(HttpServletRequest request) throws JsonProcessingException{
		ModelAndView view = new ModelAndView("main");
		TbUser user = getSessionUser();
		view.addObject("menuTree",userService.getJsonMenuByUserId(user.getUserId()));//左侧菜单
		view.addObject("moduleNames",userService.getModulesByUserId(user.getUserId()));//模块显示
	    return view;
	}
	/**
	 * 控制台
	 */
	@RequestMapping("/home")
	public ModelAndView home(){
		ModelAndView view = new ModelAndView("home");
		String czr = this.getSessionUser().getUserId();
		boolean isManager = this.getSubject().hasRole("业务经理");
		view.addObject("XTB", ryxxService.getZsTbTj(czr,isManager));
	    view.addObject("ZXT", ryxxService.getZzNdTj(czr,isManager));
		return view;
	}
	
	@RequestMapping ("/kaptcha") 
    public ModelAndView getKaptchaImage(HttpServletRequest request, HttpServletResponse response) throws Exception {  
        userService.createKaptcha(request, response);
        return null;  
    } 
	
	@RequestMapping("/403")
	public void sessionOutTime(){
		throw new UnauthorizedException("用户登陆超时，请重新登陆!");
	}
	
	@RequestMapping("/logout")
	@Log(module = "系统注销", desc = "用户登出")
	public ModelAndView logout(){
		Subject currentUser = SecurityUtils.getSubject();
		currentUser.logout();
	    return new ModelAndView("index");
	}
	
	/**
	 * 用户个人编辑
	 * 
	 */
	@RequiresAuthentication
	@RequestMapping("/editInfo") 
	public ModelAndView myEdit(){
		ModelAndView view = new ModelAndView("system/user_my");
		view.addObject("result", userService.get(StrKits.emptyToStr(request.getParameter("id"))));
	    return view;
	}
}
